package models

import "time"

type Favorites struct {
	FavoriteID  uint64    `gorm:"column:favorite_id;primary_key;auto_increment" json:"favorite_id"`
	UserID      uint64    `gorm:"column:user_id;not null" json:"user_id" `
	RecipeID    uint64    `gorm:"column:recipe_id;not null" json:"recipe_id"`
	CollectDate time.Time `gorm:"column:collect_date;" json:"collect_date" `
}

func (favor *Favorites) Create() error {
	return DB.Self.Create(&favor).Error
}

func Delete(userId, recipeId uint64) error {
	var favor Favorites
	return DB.Self.Where("user_id = ? AND recipe_id = ?", userId, recipeId).Delete(&favor).Error
}

// 查询个人所有收藏
func GetFavor(userId uint64) ([]*Favorites, error) {
	favoriteList := make([]*Favorites, 0)
	err := DB.Self.Where("user_id = ?", userId).Find(&favoriteList).Error
	return favoriteList, err
}

func CheckFavor(userId, recipeId uint64) bool {
	var favor Favorites
	err := DB.Self.Where("user_id = ? AND recipe_id = ?", userId, recipeId).First(&favor).Error
	if err != nil {
		return true
	} else {
		return false
	}
}
